﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvoWars2.Stuffs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using EvoWars.Stuffs.TankStuff;
using EvoWars2.Stuffs.TankStuff;

namespace EvoWars.Stuffs
{
    public class GameController
    {
        public World TheWorld { get; set; }
        public Camera2D Camera { get; set; }
        public Tank HumanPlayer { get; set; }


        public GameController()
        {
            TheWorld = new World();
            Camera = new Camera2D();
            Camera.Width = 800;
            Camera.Height = 800;

            TheWorld.Width = 1000;
            TheWorld.Height = 1000;

            HumanPlayer = TankFactory.MakeDefaultTank(TheWorld);
            HumanPlayer.Name = "Bob";
            HumanPlayer.IsHumanControlled = true;
            Vector2 startPos = new Vector2(100f, 100f);
            HumanPlayer.Position = startPos;

            TheWorld.Players.Add(HumanPlayer);



            int compCount = 5;
            Random rand = new Random();
            for (int i = 0; i < compCount; i++)
            {
                Tank computer = TankFactory.MakeWandererAITank(TheWorld);
                computer.Name = "Fred" + i.ToString();
                computer.IsHumanControlled = false;

                float xPos = (float)(rand.NextDouble() * (TheWorld.Width));// - computer.Texture.Width));
                float yPos = (float)(rand.NextDouble() * (TheWorld.Height));// - computer.Texture.Height));

                startPos = new Vector2(xPos, yPos);
                computer.Position = startPos;
                TheWorld.Players.Add(computer);
            }


        }


        public void Initialize()
        {
            Vector2 cornerOfTexture = new Vector2(HumanPlayer.Texture.Width, HumanPlayer.Texture.Height);
            cornerOfTexture = cornerOfTexture * .5f;
            Camera.CenterOnPoint(HumanPlayer.Position + cornerOfTexture);


        }

        public void Update(KeyboardState keyboard, GameTime gameTime)
        {
            //process keyboard input
            // Move the player left and right with arrow keys
            Vector2 moveVelocity = new Vector2();
            float amountToMove = (float)HumanPlayer.Attributes.MaxSpeed;
            if (keyboard.IsKeyDown(Keys.Left))
            {
                moveVelocity.X -= amountToMove;

            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                moveVelocity.X += amountToMove;

            }
            if (keyboard.IsKeyDown(Keys.Up))
            {
                moveVelocity.Y -= amountToMove;

            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                moveVelocity.Y += amountToMove;
            }

            if (moveVelocity.Length() > (float)HumanPlayer.Attributes.MaxSpeed)
            {
                moveVelocity.Normalize();
                moveVelocity = moveVelocity * (float)HumanPlayer.Attributes.MaxSpeed;
            }
            HumanPlayer.CurrentVelocity = moveVelocity;
            Vector2 newPosition = HumanPlayer.Position + moveVelocity;



            // Prevent the person from moving off of the screen
            newPosition.X = MathHelper.Clamp(newPosition.X, 0f + HumanPlayer.Texture.Width / 2f, (float)(TheWorld.Width - HumanPlayer.Texture.Width / 2f));
            newPosition.Y = MathHelper.Clamp(newPosition.Y, 0f + HumanPlayer.Texture.Height / 2f, (float)(TheWorld.Height - HumanPlayer.Texture.Height / 2f));


            HumanPlayer.Position = newPosition;

            foreach (Tank player in TheWorld.Players)
            {
                if (player != HumanPlayer)
                {
                    player.DoAIUpdate(gameTime.ElapsedGameTime);
                }
            }

            //update moving objects
            foreach (Tank player in TheWorld.Players)
            {
                if (player != HumanPlayer)
                {
                    player.Position = player.Position + player.CurrentVelocity;
                }
            }



            //do collision detection
            CollisionDetection2D colDetect = new CollisionDetection2D();
            CollisionResponce2D colResponce = new CollisionResponce2D();

            List<Rectangle> obstacleList = new List<Rectangle>();
            foreach (Tank player in TheWorld.Players)
            {
                if (player != HumanPlayer)
                {
                    obstacleList.Add(player.BoundingBox);
                }
            }



            while (colDetect.IsCollidingWithAny(HumanPlayer.BoundingBox, obstacleList))
            {
                List<Rectangle> collidingList = colDetect.GetCollidingWith(HumanPlayer.BoundingBox, obstacleList);
                Vector2 responceVector = colResponce.GetResponceVector(HumanPlayer.BoundingBox, collidingList[0]);
                HumanPlayer.Position = HumanPlayer.Position + responceVector;
            }





            //update camera position
            Vector2 cornerOfTexture = new Vector2(HumanPlayer.Texture.Width, HumanPlayer.Texture.Height);
            cornerOfTexture = cornerOfTexture * .5f;
            Camera.CenterOnPoint(HumanPlayer.Position + cornerOfTexture);



            //update game state (process damage take/regen-ed, kill tanks, clear dead tanks, etc)
            foreach (Tank player in TheWorld.Players)
            {
                player.UpdateCooldown(gameTime.ElapsedGameTime);
            }



        }








    }
}
